package kr.hkit.powerjava.chther22.work;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;

import javax.swing.JOptionPane;

public class TestMain {
	public static void main(String[] args) {
//		ArrayList<departmaent> deptList = new ArrayList<>();
//		DepartmentDAO deptDao = new DepartmentDAO(deptList);
//		deptDao.addItem(new department(1,"컴공",4));
//		
//		deparDao.prnList();
		
		
		ArrayList<Student> stdList = new ArrayList<>();
		
		initlist(stdList);
		StudentManager sm = new StudentManager(stdList);
		Student std;
		int res = 0;
		do {
			System.out.println("메뉴선택");
			prnlist(stdList);
			try {
				res = Integer.parseInt(JOptionPane
						.showInputDialog("1.추가  2.검색 3.수정 4.삭제 5.리스트 6.종료"));
			} catch (NumberFormatException e) {
				// TODO: handle exception
				res = 6;
			}
			switch (res) {
				case 1:
					std=(inputStdInfo(true));
					sm.addStudent(std);
					break;
				case 2:
					std=(inputStdInfo(false));
					sm.searchstd(std);
					break;
				case 3:
					std=(inputStdInfo(true));
					sm.setStudent(std);
					break;
				case 4:
					// sm.delStudent();
					break;
				case 5:
					prnlist(stdList);
					break;
				default:
					System.out.println("종료");
					break;
			}
		} while (res < 6);

		// ArrayList<Student> stdList = new ArrayList<>();
		// initlist(stdList);
		//
		// StudentManager sm = new StudentManager(stdList);
		// sm.prnList();
		// Student std =new Student(stdList.size()+1,"제시카",90,46,55);
		//
		//
		// sm.addStudent(std);
		// sm.prnList();

		// sm.delStudent("1");
		// sm.prnList();
		
		  Student stud = new Student(2, "이민정");
		  System.out.println(stdList.lastIndexOf(stud)+"하하하"+stdList.contains(stud)); Collections.sort(stdList);
		  prnheader(); prnlist(stdList); Comparator<Student> comp = new
		  Comparator<Student>() {
		  
		  @Override
		  public int compare(Student o1, Student o2) { // TODO
		
			 return o1.getMathSocre()-o2.getMathSocre(); }
		  
		  }; Collections.sort(stdList, comp); prnheader(); prnlist(stdList);
		 
	}

	
	  private static void prnheader() { // TODO Auto-generated method stub
	  System.out.printf("%4s %4s %4s%4s %4s %4s %5.2s", "학번", "이름", "국어", "영어",
	  "수학", "총점", "평균");
	  
	  }
	 
	private static Student inputStdInfo(boolean gubun) {
		if (gubun) {
			String std = JOptionPane.showInputDialog("번호 이름 국어 영어 수학");
			StringTokenizer st = new StringTokenizer(std);
			int stdNum = Integer.parseInt(st.nextToken());
			String stdName = st.nextToken();
			int kor = Integer.parseInt(st.nextToken());
			int eng = Integer.parseInt(st.nextToken());
			int math = Integer.parseInt(st.nextToken());
			return new Student(stdNum, stdName, kor, eng, math);
		} else {
			String std = JOptionPane.showInputDialog("번호 이름");
			StringTokenizer st = new StringTokenizer(std);
			int stdNum = Integer.parseInt(st.nextToken());
			String stdName = st.nextToken();
			return new Student(stdNum, stdName);
		}
	}

	private static void prnlist(ArrayList<Student> stdList) {
		// TODO Auto-generated method stub
		System.out.println();
		for (Student std : stdList) {
			System.out.println(std);
		}
	}

	private static void initlist(ArrayList<Student> stdList) {
		// TODO Auto-generated method stub
		stdList.add(new Student(1, "신우석", 100, 51, 30));
		stdList.add(new Student(2, "신우석1", 10, 50, 20));
		stdList.add(new Student(3, "신우석2", 90, 80, 100));
		stdList.add(new Student(4, "신우석3", 100, 30, 50));
		stdList.add(new Student(5, "신우석4", 35, 100, 30));
	}
}
